/*
 * uart_485.c
 *
 *  Created on: 2023年7月7日
 *      Author: 86176
 */
#include "pin_mux.h"
#include "clock_config.h"
#include "board.h"
#include "uart_485.h"
#include "fsl_debug_console.h"
#include "wit_c_sdk.h"
/*******************************************************************************
 * Definitions
 ******************************************************************************/
#define EXAMPLE_LPUART                 LPUART3
#define EXAMPLE_LPUART_CLK_FREQ        BOARD_DebugConsoleSrcFreq()
#define EXAMPLE_LPUART_IRQHandler      LPUART3_IRQHandler
#define EXAMPLE_LPUART_IRQn            LPUART3_IRQn


/*! @brief Ring buffer size (Unit: Byte). */
#define DEMO_RING_BUFFER_SIZE 16

/*! @brief Ring buffer to save received data. */

/*******************************************************************************
 * Prototypes
 ******************************************************************************/
/*******************************************************************************
 * Variables
 ******************************************************************************/

uint8_t g_tipString[] =
    "Lpuart functional API interrupt example\r\nBoard receives characters then sends them out\r\nNow please input:\r\n";

/*
  Ring buffer for data input and output, in this example, input data are saved
  to ring buffer in IRQ handler. The main function polls the ring buffer status,
  if there are new data, then send them out.
  Ring buffer full: (((rxIndex + 1) % DEMO_RING_BUFFER_SIZE) == txIndex)
  Ring buffer empty: (rxIndex == txIndex)
*/
uint8_t demoRingBuffer[DEMO_RING_BUFFER_SIZE];
volatile uint16_t txIndex; /* Index of the data to send out. */
volatile uint16_t rxIndex; /* Index of the memory to save new arrived data. */

/*******************************************************************************
 * Code
 ******************************************************************************/

void EXAMPLE_LPUART_IRQHandler(void)
{
    uint8_t data;
    uint16_t tmprxIndex = rxIndex;
    uint16_t tmptxIndex = txIndex;

    /* If new data arrived. */
#if defined(LP_FLEXCOMM_PSELID_UARTPRESENT_MASK)
    if ((kLPUART_RxDataRegFullInterruptFlag)&LPUART_GetStatusFlags(EXAMPLE_LPUART))
#else
    if ((kLPUART_RxDataRegFullFlag)&LPUART_GetStatusFlags(EXAMPLE_LPUART))
#endif
    {
        data = LPUART_ReadByte(EXAMPLE_LPUART);

        WitSerialDataIn(data);

//        LPUART_ClearStatusFlags(EXAMPLE_LPUART,kLPUART_RxDataRegFullInterruptFlag);
////        /* If ring buffer is not full, add data to ring buffer. */
////        if (((tmprxIndex + 1) % DEMO_RING_BUFFER_SIZE) != tmptxIndex)
////        {
////            demoRingBuffer[rxIndex] = data;
////            rxIndex++;
////            rxIndex %= DEMO_RING_BUFFER_SIZE;
////        }
    }
    SDK_ISR_EXIT_BARRIER;
}

void Uart485_init()
{
	lpuart_config_t config;
   LPUART_GetDefaultConfig(&config);
   config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE;
   config.enableTx     = true;
   config.enableRx     = true;

   LPUART_Init(EXAMPLE_LPUART, &config, EXAMPLE_LPUART_CLK_FREQ);



   /* Enable RX interrupt. */
   LPUART_EnableInterrupts(EXAMPLE_LPUART, kLPUART_RxDataRegFullInterruptEnable);
   EnableIRQ(EXAMPLE_LPUART_IRQn);
}

void Uart485_send(uint8_t *p_data, uint32_t uiSize)
{
	LPUART_WriteBlocking(EXAMPLE_LPUART,p_data, uiSize);
}

